<template>
  <div id="app">
    <div class="header">网页头部</div>
    <div class="content">
      <!-- 使用子组件标签 -->
      <CartHeader :list="list" @changeAll="changeAll" />
      <CartList :list="list"/>
      <!-- 子组件点击clearCart，父组件的自定义事件clearCart接收到，并且传了clearCart方法给子组件。delAllSelect事件也是同理-->
      <CartFooter :list="list" @clearCart="clearCart" @delAllSelect="delAllSelect" />
    </div>
    <div class="footer">网页底部</div>
  </div>
</template>

<script>
  //引入list数据
 import {list} from "./data.js";
 //引入子组件
 import CartHeader from "./components/CartHeader.vue";
 import CartList from "./components/CartList.vue";
 import CartFooter from "./components/CartFooter.vue";
 
export default {
  name: 'App',
  data(){
    return {
      list,
    };
  },

  methods:{
    //删除子组件按钮的方法函数，收到生命钩子传的id
    delOneGood(id){
      this.list = this.list.filter((item)=>item.id !== id)  //filter把符合条件的item留下
    },

    //切换全选、全不选
    changeAll(value){
      this.list.forEach((item)=>{
        item.select=value;
      })
    },

    //清空购物车--子组件的按钮事件---把list等于一个空数组
    clearCart(){
      this.list=[];
    },

    //删除所有选中----filter过滤数组---符合条件的item留下(select为假),然后重新赋值给list
    delAllSelect(){
      this.list = this.list.filter((item)=> !item.select)
    }
  },

  //注册子组件
  components:{
    CartHeader,
    CartList,
    CartFooter,
  },

  //生命钩子
  mounted(){
    //接收到bus传入的id，然后调用当前的delOneGood方法，把id传参给该方法
    this.$bus.$on("delOneGood",this.delOneGood)
  },

  
};

</script>

<style lang="scss">
* {
  margin: 0;
  padding: 0;
}

ul,
ol,
li {
  list-style: none;
}

input,
button,
textarea {
  outline: none;
  border: none;
}

a,
a:hover,
a:target,
a:link,
a:active {
  text-decoration: none;
  color: #333;
}

a,
img {
  width: 100%;
  height: 100%;
  display: block;
}
.header,
.footer {
  width: 1200px;
  height: 80px;
  background-color: skyblue;
  color: #fff;
  font-size: 36px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
}

.footer {
  height: 300px;
  font-size: 100px;
}
.content {
  width: 1200px;
  padding: 10px 0;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

  
</style>
